﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class DamageReportedApp : IDamageReported
    {
        private string dbname = "Systems";

        public void Add(DamageReportedEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_DamageReported(");
            builder.Append("ChainNum,WarehouseId,DRNum,UserId,DRTime,IsCheck,CheckMan,CheckTime,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("" + model.WarehouseId + ",");
            builder.Append("'" + model.DRNum + "',");
            builder.Append("" + model.UserId + ",");
            builder.Append("'" + model.DRTime + "',");
            builder.Append("" + model.IsCheck + ",");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int DRId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_DamageReported ");
            builder.Append(" where DRId=" + DRId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_DamageReported ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by DRId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "DRId", "T_DamageReported", swhere);
        }

        public DamageReportedEnt GetModel(int DRId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select DRId,ChainNum,WarehouseId,DRNum,UserId,DRTime,IsCheck,CheckMan,CheckTime,Remark from T_DamageReported ");
            builder.Append(" where DRId=" + DRId);
            DamageReportedEnt ent = new DamageReportedEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.DRId = DRId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                if (table.Rows[0]["WarehouseId"].ToString() != "")
                {
                    ent.WarehouseId = int.Parse(table.Rows[0]["WarehouseId"].ToString());
                }
                ent.DRNum = table.Rows[0]["DRNum"].ToString();
                if (table.Rows[0]["UserId"].ToString() != "")
                {
                    ent.UserId = int.Parse(table.Rows[0]["UserId"].ToString());
                }
                if (table.Rows[0]["DRTime"].ToString() != "")
                {
                    ent.DRTime = DateTime.Parse(table.Rows[0]["DRTime"].ToString());
                }
                if (table.Rows[0]["IsCheck"].ToString() != "")
                {
                    ent.IsCheck = int.Parse(table.Rows[0]["IsCheck"].ToString());
                }
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_DamageReported ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "DRId");
        }

        public void Update(DamageReportedEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_DamageReported set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("WarehouseId=" + model.WarehouseId + ",");
            builder.Append("DRNum='" + model.DRNum + "',");
            builder.Append("UserId=" + model.UserId + ",");
            builder.Append("DRTime='" + model.DRTime + "',");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where DRId=" + model.DRId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

